require 'net/http'
require 'uri'
require 'rss'

class SubscriptionsController < ApplicationController
  def index
    @subscriptions = Subscription.all
  end


  # Obrazac za unos subscriptiona
  def new
    @subscription = Subscription.new
  end

  def edit
    @subscription = Subscription.find params[:id]
  end

  # Stvaranje subscriptiona
  def create
    @subscription = Subscription.new(params[:subscription])
    @subscription.confirmed = false
    if @subscription.save
      redirect_to(@subscription, :notice => 'Subscription was successfully created.') 
    else
      render :action => "new"
    end
  end
  
  def update
    @subscription = Subscription.find(params[:id])

    if @subscription.update_attributes(params[:subscription])
      redirect_to(@subscription, :notice => 'Subscription was successfully updated.')
    else
      render :action => "edit"
    end
  end
  
  
  def show
    @subscription = Subscription.find params[:id]
  end
  
  # nova pretplata
  def subscribe
    @subscription = Subscription.find params[:id]
    hub_url = @subscription.hub
    topic = @subscription.url
    callback_url = 'http://udip.hr:8084/subscriptions/recieve'
    mode = 'subscribe'
    verification_type = 'sync'    
    url = URI.parse( hub_url )
    
    post_data = {'hub.callback' => callback_url, 'hub.mode' => mode,'hub.topic' => topic, 'hub.verify' => verification_type}
    res = Net::HTTP.post_form( url, post_data)

    puts "hub_url: " + hub_url
    puts "hub.callback: " + callback_url
    puts "hub.mode: " + mode
    puts "hub.topic: " + topic
    puts "hub.verify: " + verification_type
    
    @callback_url = callback_url
    @hub_url = hub_url
    @post_data = post_data
    @result = res.body
    
    unless @result.nil? || @result.empty?
      @subscription.confirmed = false
      @subscription.save
    end
  end
  
  # ODJAVA pretplate
  def unsubscribe
    @subscription = Subscription.find params[:id]
    hub_url = @subscription.hub
    topic = @subscription.url
    
    callback_url = 'http://udip.hr:8084/subscriptions/recieve'
    mode = 'unsubscribe'
    verification_type = 'async'    
    url = URI.parse( hub_url )
    post_data = {'hub.callback' => callback_url, 'hub.mode' => mode,'hub.topic' => topic, 'hub.verify' => verification_type}

    res = Net::HTTP.post_form( url, post_data)
  end
  
  # potrvrda pretplate
  def request_confirmation
    topic = params['hub.topic']
    challange = params['hub.challenge']
    mode = params['hub.mode']
    subscription = Subscription.where(:url => topic).first
    
    puts 'New confirmation request!'
    puts mode
    puts topic
    puts 'Challange: '+challange
    
    if subscription.nil?
      head :bad_request
    else
      subscription.confirmed = (mode == 'subscribe')
      subscription.updated_at = Time.now
      if subscription.save
        render :text => challange
      else
        head :bad_request
      end
    end
    
    
  end
  
  # prihvacanje sadrzaja
  def new_content
    content = request.body.read 
    rss = SimpleRSS.parse content

    rss.items.each do |item|
      post = Post.new
      post.title = item.title
      post.content = item.content
      post.save
    end
    
    puts 'New content!!!'
    puts content
    render :text => "thx!"
  end
end
